package com.wrial.simple;
/*
 * @Author  Wrial
 * @Date Created in 13:33 2020/3/29
 * @Description 二叉搜索树的范围和
 */

public class LeetCode_938 {

    int sum = 0;

    public int rangeSumBST(TreeNode root, int L, int R) {
        if (root != null) {

            if (root.val >= L && root.val <= R) sum += root.val; //中
            // 左
            if (root.val > L) {
                rangeSumBST(root.left, L, R);
            }
            // 右
            if (root.val < R) {
                rangeSumBST(root.right, L, R);
            }
        }
        return sum;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }


}
